<?php

namespace App\Libs;

/** 京东sdk封装 */
class JdSdkLib
{
    /** @var string 请求地址 */
    private $formalUrl = 'https://router.jd.com/api?';

    /**
     * 发送京东请求
     * @param $method
     * @param $paramJson
     * @return array|mixed
     */
    private function sendRequest($method, $paramJson)
    {
        $v = '1.0';
        $appKey = config('lcm.jd_app_key');
        $signMethod = 'md5';
        $format = 'json';
        $timestamp = date('Y-m-d H:i:s');
        $data = [
            'method' => $method,
            'app_key' => $appKey,
            'timestamp' => $timestamp,
            'format' => $format,
            'v' => $v,
            'sign_method' => $signMethod,
            'param_json' => $paramJson
        ];
        ksort($data);
        $signature = config('lcm.jd_secret_key');
        foreach ($data as $key => $value) {
            $signature .= $key . $value;
        }

        $signature .= config('lcm.jd_secret_key');
        $sign = strtoupper(md5($signature));
        $queryData = [
            'v' => $v,
            'method' => $method,
            'access_token' => '',
            'app_key' => $appKey,
            'sign_method' => 'md5',
            'format' => 'json',
            'timestamp' => $timestamp,
            'sign' => $sign,
            'param_json' => $paramJson
        ];
        $applyRequest = new ApplyRequestLib();
        $resultKey = str_replace('.', '_', $method) . '_response';
        $result = $applyRequest->postUrl($this->formalUrl . http_build_query($queryData), []);
        return isset($result[$resultKey]) ? $result[$resultKey] : ['code' => 1];
    }

    /** 精选产品 */
    public function boutiqueGoods($eliteId, $pageIndex, $pageSize)
    {
        $params = [
            'goodsReq' => [
                'eliteId' => $eliteId,
                'pageIndex' => $pageIndex,
                'pageSize' => $pageSize,
            ]
        ];

        $method = 'jd.union.open.goods.jingfen.query';
        $paramJson = json_encode($params);
        $res = $this->sendRequest($method, $paramJson);
        if ($res['code'] == 0) {
            $result = json_decode($res['result'], true);
            if ($result['code'] != 200) {
                return [];
            } else {
                return $result;
            }
        } else {
            return [];
        }
    }

    /**
     * 获取类目(传入 0, 0表示列表一级类目)
     * @param $parentId int 父类id
     * @param $grade int 列表的等级 0, 1, 2
     * @return array|mixed
     */
    public function getCategory($parentId, $grade)
    {
        $params = [
            'req' => [
                'parentId' => $parentId,
                'grade' => $grade
            ]
        ];

        $method = 'jd.union.open.category.goods.get';
        $paramJson = json_encode($params);
        $res = $this->sendRequest($method, $paramJson);
        if ($res['code'] == 0) {
            $result = json_decode($res['result'], true);
            if ($result['code'] != 200) {
                return [];
            } else {
                return $result['data'];
            }
        } else {
            return [];
        }
    }

    /**
     * 转链
     * @param $materialId string 转链地址
     * @param $positionId string 推广位id
     * @return array|mixed
     */
    public function promotion($materialId, $positionId)
    {
        $params = ['promotionCodeReq' =>
            [
                'materialId' => $materialId,
                'positionId' => $positionId
            ]
        ];
        $method = 'jd.union.open.promotion.common.get';
        $paramJson = json_encode($params);
        $res = $this->sendRequest($method, $paramJson);
        var_dump($res);exit();
        if ($res['code'] == 0) {
            $result = json_decode($res['result'], true);
            if ($result['code'] != 200) {
                return [];
            } else {
                return $result['data'];
            }
        } else {
            return [];
        }
    }
}
